//
//  UIButtomExt.swift
//  test-ios
//
//  Created by wyz5204 on 2023/6/6.
//

import Foundation
import UIKit
import RxSwift

//MARK: UIButton
extension UIButton {
    /// - Description 统一设置Button属性
    /// - Parameters:
    ///   - title: 文字内容
    ///   - size: titleLabel 字体大小
    ///   - weight: titleLabel 字重
    ///   - titleColor: titleLabel color
    ///   - imageNamed: 按钮图片
    ///   - state: 按钮状态
    /// - Returns: UILabel实例化对象
    class func glButton(title: String, size: CGFloat, weight: UIFont.Weight, titleColor: UIColor, imageNamed: String? = nil, state: UIControl.State) -> UIButton {
        let button = UIButton.init(type: .custom)
        button.setTitle(title, for: state)
        button.setTitleColor(titleColor, for: state)
        if let imageNamed = imageNamed {
            button.setImage(imageNamed.glImage, for: state)
        }
        button.titleLabel?.font = UIFont.systemFont(ofSize: size, weight: weight)
        return button
    }
    
    /// button 图片旋转动画
    func imageViewRotation() {
        //创建动画
        let anim = CABasicAnimation()
        //一个重要的设置:就是keyPath
        //旋转动画一定要设置为transform.rotation,不能写错
        anim.keyPath = "transform.rotation"
        //目标值
        if self.isSelected {
            anim.toValue = 0
        }else {
            anim.toValue = Double.pi
        }
        //动画时长
        anim.duration = 0.25
        //以下两句可以设置动画结束时 layer停在toValue这里
        anim.isRemovedOnCompletion = false
        anim.fillMode = CAMediaTimingFillMode.forwards
        //添加动画到layer层上
        self.imageView?.layer.add(anim, forKey: nil)
    }
    
    /// 提供多个运行时的key
    struct GLButtonKey {
        static let buttonKey = UnsafeRawPointer.init(bitPattern: "gl_key".hashValue)
    }
    /// 需要扩充的边距 (关联属性)
    var hitEdgeInsets: UIEdgeInsets? {
        set {
            objc_setAssociatedObject(self, GLButtonKey.buttonKey!, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY)
        }
        get {
            return objc_getAssociatedObject(self, GLButtonKey.buttonKey!) as? UIEdgeInsets ?? UIEdgeInsets.zero
        }
    }
    
    ///是否响应
    open override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        if (hitEdgeInsets! == UIEdgeInsets.zero) || !isEnabled || isHidden {
            return super.point(inside: point, with: event)
        }
        return bounds.inset(by: hitEdgeInsets!).contains(point)
    }
    
    enum GLButtonImagePosition {
        case top          //图片在上，文字在下，垂直居中对齐
        case bottom       //图片在下，文字在上，垂直居中对齐
        case left         //图片在左，文字在右，水平居中对齐
        case right        //图片在右，文字在左，水平居中对齐
    }
    
    /// - Description 设置Button图片的位置
    /// - Parameters:
    ///   - style: 图片位置
    ///   - spacing: 按钮图片与文字之间的间隔
    func imagePosition(style: GLButtonImagePosition, spacing: CGFloat) {
        //得到imageView和titleLabel的宽高
        let imageWidth = self.imageView?.frame.size.width
        let imageHeight = self.imageView?.frame.size.height
        
        var labelWidth: CGFloat! = 0.0
        var labelHeight: CGFloat! = 0.0
        
        labelWidth = self.titleLabel?.intrinsicContentSize.width
        labelHeight = self.titleLabel?.intrinsicContentSize.height
        
        //初始化imageEdgeInsets和labelEdgeInsets
        var imageEdgeInsets = UIEdgeInsets.zero
        var labelEdgeInsets = UIEdgeInsets.zero
        
        //根据style和space得到imageEdgeInsets和labelEdgeInsets的值
        switch style {
        case .top:
            //上 左 下 右
            imageEdgeInsets = UIEdgeInsets(top: -labelHeight-spacing/2, left: 0, bottom: 0, right: -labelWidth)
            labelEdgeInsets = UIEdgeInsets(top: 0, left: -imageWidth!, bottom: -imageHeight!-spacing/2, right: 0)
            break;
            
        case .left:
            imageEdgeInsets = UIEdgeInsets(top: 0, left: -spacing/2, bottom: 0, right: spacing)
            labelEdgeInsets = UIEdgeInsets(top: 0, left: spacing/2, bottom: 0, right: -spacing/2)
            break;
            
        case .bottom:
            imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: -labelHeight!-spacing/2, right: -labelWidth)
            labelEdgeInsets = UIEdgeInsets(top: -imageHeight!-spacing/2, left: -imageWidth!, bottom: 0, right: 0)
            break;
            
        case .right:
            imageEdgeInsets = UIEdgeInsets(top: 0, left: labelWidth+spacing/2, bottom: 0, right: -labelWidth-spacing/2)
            labelEdgeInsets = UIEdgeInsets(top: 0, left: -imageWidth!-spacing/2, bottom: 0, right: imageWidth!+spacing/2)
            break;
            
        }
        
        self.titleEdgeInsets = labelEdgeInsets
        self.imageEdgeInsets = imageEdgeInsets
        
    }
    
    func setTapListener(_ block :@escaping (UIButton) -> Void) -> Disposable{
        return self.rx.tap.asObservable().subscribe { event in
            block(self)
        }
    }
    
    func setTitleAndColor(_ text : String , textState: UIControl.State = .normal ,
                          color: UIColor = .black,  colorState: UIControl.State = .normal){
        self.setTitle(text, for: state)
        self.setTitleColor(color, for: colorState)
    }
}
